package logic

import (
	"context"
	"strconv"
	"usershop/user_srv/user"

	"usershop/api/internal/svc"
	"usershop/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateImageLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUpdateImageLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateImageLogic {
	return &UpdateImageLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UpdateImageLogic) UpdateImage(req *types.UpdateImageRequest) (resp *types.ResMsg, err error) {
	// todo: 修改用户头像
	userID := l.ctx.Value("UserID").(string)
	uin, _ := strconv.Atoi(userID)

	_, err = l.svcCtx.UserSrv.UpdateAvatar(l.ctx, &user.UpdateAvatarRequest{
		UserID:    int64(uin),
		AvatarUrl: req.ImageUrl,
	})
	if err != nil {
		return &types.ResMsg{
			Cre:  -1,
			Msg:  "修改失败",
			Data: nil,
		}, err
	}
	return &types.ResMsg{
		Cre:  0,
		Msg:  "更新成功",
		Data: nil,
	}, err
}
